package cn.kpt.biz.controller;

import cn.kpt.beans.dto.Dto;
import cn.kpt.beans.pojo.ItripUser;
import cn.kpt.beans.pojo.ItripUserLinkUser;
import cn.kpt.beans.vo.userinfo.ItripAddUserLinkUserVO;
import cn.kpt.beans.vo.userinfo.ItripSearchUserLinkUserVO;
import cn.kpt.biz.service.LinkUserService;
import cn.kpt.utils.DtoUtil;
import cn.kpt.utils.ErrorCode;
import cn.kpt.utils.ValidationToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @ClassName UserInfoController
 * @Description TODO
 * @Author kangpengtao
 * @Date 2019/10/21 16:13
 */
@Api(value = "api")
@RestController
@RequestMapping("/api/userinfo")
public class UserInfoController {

    @Resource
    private LinkUserService linkUserService;
    @Resource
    private ValidationToken validationToken;

    /**
     * 订单页面-添加联系人
     * @param vo
     * @param token
     * @return
     */
    @ApiOperation(value = "新增常用联系人接口", httpMethod = "POST",
            protocols = "HTTP",produces = "application/json",
            response = Dto.class,notes = "新增常用联系人信息"+
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>错误码：</p>"+
            "<p>100411 : 新增常用联系人失败 </p>"+
            "<p>100412 : 不能提交空，请填写常用联系人信息</p>"+
            "<p>100000 : token失效，请重登录 </p>")
    @RequestMapping(value = "/adduserlinkuser",method = RequestMethod.POST)
    public Dto doAddUserLinkUser(@RequestBody ItripAddUserLinkUserVO vo, @RequestHeader String token){
        try {
            // 省略表单验证
            ItripUser currentUser = validationToken.getCurrentUser("token:" + token);

            ItripUserLinkUser itripUserLinkUser = new ItripUserLinkUser();
            itripUserLinkUser.setUserId(currentUser.getId());
            itripUserLinkUser.setLinkIdCard(vo.getLinkIdCard());
            itripUserLinkUser.setLinkIdCardType(vo.getLinkIdCardType());
            itripUserLinkUser.setLinkPhone(vo.getLinkPhone());
            itripUserLinkUser.setLinkUserName(vo.getLinkUserName());
            itripUserLinkUser.setCreatedBy(currentUser.getId());
            itripUserLinkUser.setCreationDate(new Date());

            linkUserService.addLinkUser(itripUserLinkUser);
            return DtoUtil.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("添加联系人失败！",ErrorCode.BIZ_UNKNOWN);
        }
    }
    /**
     * 订单页面-查询联系人
     * @param token
     * @return
     */
    @ApiOperation(value = "查询常用联系人接口", httpMethod = "POST",
            protocols = "HTTP",produces = "application/json",
            response = Dto.class,notes = "查询常用联系人信息(可根据联系人姓名进行模糊查询)"+
            "<p>若不根据联系人姓名进行查询，不输入参数即可 | 若根据联系人姓名进行查询，须进行相应的入参，比如：{\"linkUserName\":\"张三\"}</p>" +
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>错误码：</p>"+
            "<p>100401 : 获取常用联系人信息失败 </p>"+
            "<p>100000 : token失效，请重登录</p>")
    @RequestMapping(value = "/queryuserlinkuser",method = RequestMethod.POST)
    public Dto doQueryLinkUser(@RequestHeader String token){
        try {
            ItripUser currentUser = validationToken.getCurrentUser("token:" + token);
            List<ItripSearchUserLinkUserVO> vos = linkUserService.queryLinkUser(currentUser.getId());
            return DtoUtil.returnDataSuccess(vos);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("查询联系人失败！", ErrorCode.BIZ_UNKNOWN);
        }
    }
}
